#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 30;
const int inf  = 0x3f3f3f3f;
int T,n,data[maxn][maxn],ans,ret=inf;
int rev[maxn][maxn];
#define min(a,b) ((a)<(b)?(a):(b))
void update_ans()
{
	ans = 0;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int now = rev[i][j+1] + rev[i-1][j] + rev[i][j-1];
			if(now & 1)
			{
				rev[i+1][j] = 1;
			}
			else
			{
				if(data[i+1][j]==1)
					return;
				rev[i+1][j] = 0;
			}
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(data[i][j] != rev[i][j])
			{
				ans++;
			}
	ret = min(ret , ans);
}
void dfs(int x)
{
	if(x==n+1)
	{
		update_ans();
		return;
	}
	if(data[1][x]==1)
	{
		rev[1][x] = 1;
		dfs(x+1);
	}
	else
	{
		rev[1][x] = 1;
		dfs(x+1);
		rev[1][x] = 0;
		dfs(x+1);
	}
}
int main()
{
	scanf("%d",&T);
	for(int t=1;t<=T;t++)
	{
		ret = inf;
		memset(rev,0,sizeof(rev));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&data[i][j]);
			}
		}
		dfs(1);
		if(ret == inf)
		printf("Case %d: %d\n",t,-1);
		else
		printf("Case %d: %d\n",t,ret);
	}
	return 0;
}